import modelExtend from 'dva-model-extend'
import api from 'api'
import { pageModel } from 'utils/model'
const { pathToRegexp } = require("path-to-regexp")
const { queryTemporalTableColList, updateTemporalTableCol, createTemporalTableCol,deleteTemporalTableCol,queryTemporalTableOptions
        ,queryTemporalTableColGetData} = api
/**
 *
   //相关api信息
 queryTemporalTableColList: `GET ${prefix}/admin/temporalTableCol/list`,
 updateTemporalTableCol: `PUT ${prefix}/admin/temporalTableCol`,
 createTemporalTableCol: `POST ${prefix}/admin/temporalTableCol`,
 deleteTemporalTableCol: `DELETE ${prefix}/admin/temporalTableCol/:ids`,
 queryTemporalTableColDetails: `GET ${prefix}/admin/temporalTableCol/:id`,
 *
 */
export default modelExtend(pageModel, {
  namespace: 'temporal_table_col',
  state: {
    getData: {
      show: false,
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    }, //获取到的时序数据
    optionValueLabel: {},    options: {
      tableId: {
      },

      type: {
           '0': 'BOOLEAN',
           '1': 'INT32',
           '2': 'INT64',
           '3': 'FLOAT',
           '4': 'DOUBLE',
           '5': 'TEXT',
           '6': 'VECTOR',
           '7': 'UNKNOWN',
      },

      status: {
           '0': '禁用',
           '1': '活跃',
      },
    },
    trees: {
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (pathToRegexp('/temporal_table_col').exec(location.pathname)) {
          dispatch({
            type: 'query',
            payload: {
              pageSize: 20,
              page: 1,
              ...location.query,
            },
          })
          dispatch({
            type: 'queryTemporalTableOptions',
            payload: {},
          })
        }
      })
    },
  },

  effects: {
    *query({ payload }, { call, put }) {
      const data = yield call(queryTemporalTableColList, payload)
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.payload.list,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.payload.total,
            },
          },
        })
      } else {
        throw data
      }
    },

    //获取数据
    *queryTemporalTableColGetData({ payload }, { call, put }) {
      const data = yield call(queryTemporalTableColGetData, payload)
      if (data.success) {
        console.log("-----------------------------------")
        console.log(data)
        if(data.code === 2000) {
          yield put({
            type: 'updateState',
            payload: {
              getData: {
                show: true,
                list: data.payload,
                pagination: {
                  current: Number(payload.page) || 1,
                  pageSize: Number(payload.pageSize) || 10,
                  total: 1000000,
                },
              }
            },
          })
        } else {
          throw data;
        }

      } else {
        throw data
      }
    },
    //隐藏获取数据窗口
    *hideGetData({payload}, {call, put}) {
      yield put({
        type: 'updateState',
        payload: {
          getData: {
            show: false,
            list: [],
            pagination: {
              current: 1,
              pageSize: 10,
              total: 0,
            },
          }
        },
      })
    },
   *create({ payload }, { call, put }) {
      const data = yield call(createTemporalTableCol, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },

    *update({ payload }, { select, call, put }) {
      const data = yield call(updateTemporalTableCol, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },
    *delete({ payload }, { select, call, put }) {
      const data = yield call(deleteTemporalTableCol, {ids:payload})
      if (!data.success) {
        throw data
      }
    },
    *queryTemporalTableOptions({ payload }, { call, put }) {
      const data = yield call(queryTemporalTableOptions, payload)
      if (data.success) {
        yield put({
          type: 'queryOptions',
          payload: {
              tableId: data.payload
          },
        })
      } else {
        throw data
      }
    },  },
  reducers: {
    showModal(state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal(state) {
      return { ...state, modalVisible: false }
    },
  },
})
